/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function (nums) {
  let res = []
  let n = nums.length
  let used = []
  let combine = []
  nums.sort((a,b)=>a-b)
  function help(depth, combine, used) {
    
    if(depth === n){
      res.push([...combine])
      return 
    }
    for (let i = 0; i < n; i++) {
      if (used[i] || (i > 0 && nums[i] === nums[i - 1] && !used[i - 1])) {
        continue;
      }
      combine.push(nums[i])
      used[i] = true
      help(depth + 1, combine, used)
      used[i] = false
      combine.pop()
    }
    
  }
  help(0, combine, used)
  return res
};

var nums = [1, 1, 2]
console.log(permuteUnique(nums))
